﻿using Newtonsoft.Json;
using OS.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace OS.WebApi
{
    public class BusinessController : ApiController
    {
        public Bll_jbxx_spdaH bllJbxx_spdaH = new Bll_jbxx_spdaH();
        public Bll_zwcl_kczbH bllZwcl_kczbH = new Bll_zwcl_kczbH();
        public Bll_view_Spda bllView_Spda = new Bll_view_Spda();
        public Bll_jbxx_khdaH bllJbxx_khdaH = new Bll_jbxx_khdaH();

        public Bll_yw_pfdH bll_yw_pdbH = new Bll_yw_pfdH();
        public Bll_yw_pfdB bll_yw_pdbB = new Bll_yw_pfdB();



        public Bll_STDjLsh bllSTDjLsh = new Bll_STDjLsh();
        /// <summary>
        /// 分页获取商品档案列表(允许带条件查询)
        /// 创建人:minjie
        /// 创建时间：2020-09-19
        /// <remarks>
        /// 测试一些内容，不想将无用的东西放在接口名称当中<br/>
        /// 换行输出一下内容
        /// </remarks>
        /// </summary>
        /// <param name="pageSize">每页显示数</param>
        /// <param name="pageNumber">当前页</param>
        /// <param name="strWhere">条件 例如:  spbh='DF03447-081'</param>
        /// <returns></returns>

        [HttpGet]
        public RestPage<view_Spda_Entity> GetSpdaList(int pageSize, int pageNumber, string strWhere)
        {
            RestPage<view_Spda_Entity> dto = new RestPage<view_Spda_Entity>();
            if (strWhere == null || !Common2.CheckBadWord(strWhere))
            {
                dto.status = 1;
                int startIndex = (pageNumber - 1) * pageSize + 1;
                int endIndex = pageNumber * pageSize;
                List<view_Spda_Entity> ls = bllView_Spda.GetModelList(strWhere, "xgsj desc", startIndex, endIndex);
                int count = bllView_Spda.GetRecordCount(strWhere);
                dto.rows = ls;
                dto.total = count;
            }
            else
            {
                dto.status = 0;
                dto.msg = "条件存在sql注入风险 不允许操作！";
            }

            return dto;
        }


        [HttpGet]
        public RestPage<jbxx_spdaH_Entity> GetSpdaList_2(int pageSize, int pageNumber, string strWhere)
        {
            RestPage<jbxx_spdaH_Entity> dto = new RestPage<jbxx_spdaH_Entity>();
            if (strWhere == null || !Common2.CheckBadWord(strWhere))
            {
                dto.status = 1;
                int startIndex = (pageNumber - 1) * pageSize + 1;
                int endIndex = pageNumber * pageSize;
                List<jbxx_spdaH_Entity> ls = bllJbxx_spdaH.GetModelList(strWhere, "xgsj desc", startIndex, endIndex);
                int count = bllJbxx_spdaH.GetRecordCount(strWhere);
                dto.rows = ls;
                dto.total = count;
            }
            else
            {
                dto.status = 0;
                dto.msg = "条件存在sql注入风险 不允许操作！";
            }

            return dto;
        }

        /// <summary>
        /// 分页获取商品档案列表 
        /// 创建人:minjie
        /// 创建时间：2020-09-19
        /// </summary>
        /// <param name="pageSize">每页显示数</param>
        /// <param name="pageNumber">当前页</param>
        /// <returns></returns>
        [HttpGet]
        public RestPage<jbxx_spdaH_Entity> GetSpdaList_1(int pageSize, int pageNumber)
        {
            string strWhere = "";
            RestPage<jbxx_spdaH_Entity> dto = new RestPage<jbxx_spdaH_Entity>();
            if (!Common2.CheckBadWord(strWhere))
            {
                dto.status = 1;
                int startIndex = (pageNumber - 1) * pageSize + 1;
                int endIndex = pageNumber * pageSize;
                List<jbxx_spdaH_Entity> ls = bllJbxx_spdaH.GetModelList(strWhere, "xgsj desc", startIndex, endIndex);
                int count = bllJbxx_spdaH.GetRecordCount(strWhere);
                dto.rows = ls;
                dto.total = count;
            }
            else
            {
                dto.status = 0;
                dto.msg = "条件存在sql注入风险 不允许操作！";
            }

            return dto;
        }


        /// <summary>
        /// 分页获取库存列表（允许带条件）
        /// 创建人:minjie
        /// 创建时间：2020-09-19
        /// </summary>
        /// <param name="pageSize">每页显示数</param>
        /// <param name="pageNumber">当前页</param>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        [HttpGet]
        public RestPage<zwcl_kczbH_Entity> GetKczbList(int pageSize, int pageNumber, string strWhere)
        {
            RestPage<zwcl_kczbH_Entity> dto = new RestPage<zwcl_kczbH_Entity>();
            if (!Common2.CheckBadWord(strWhere))
            {
                dto.status = 1;
                int startIndex = (pageNumber - 1) * pageSize + 1;
                int endIndex = pageNumber * pageSize;
                List<zwcl_kczbH_Entity> ls = bllZwcl_kczbH.GetModelList("", "DjLsh desc", startIndex, endIndex);
                int count = bllJbxx_spdaH.GetRecordCount("");
                dto.rows = ls;
                dto.total = count;
            }
            else
            {
                dto.status = 0;
                dto.msg = "条件存在sql注入风险 不允许操作！";
            }

            return dto;
        }
        ///// <summary>
        ///// 分页获取库存列表(允许带条件)
        ///// 创建人:minjie
        ///// 创建时间：2020-09-19
        ///// </summary>
        ///// <param name="pageSize">每页显示数</param>
        ///// <param name="pageNumber">当前页</param>
        ///// <param name="strWhere">条件</param>
        ///// <returns></returns>
        //[HttpGet]
        //public RestPage<zwcl_kczbH_Entity> GetKczbListByWhere(int pageSize, int pageNumber, string strWhere)
        //{
        //    RestPage<zwcl_kczbH_Entity> dto = new RestPage<zwcl_kczbH_Entity>();
        //    if (!Common2.CheckBadWord(strWhere))
        //    {
        //        dto.status = 1;
        //        int startIndex = (pageNumber - 1) * pageSize + 1;
        //        int endIndex = pageNumber * pageSize;
        //        List<zwcl_kczbH_Entity> ls = bllZwcl_kczbH.GetModelList("", "DjLsh desc", startIndex, endIndex);
        //        int count = bllJbxx_spdaH.GetRecordCount("");
        //        dto.rows = ls;
        //        dto.total = count;
        //    }
        //    else
        //    {
        //        dto.status = 0;
        //        dto.msg = "条件存在sql注入风险 不允许操作！";
        //    }
        //    return dto;
        //}
        /// <summary>
        /// 单独获取商品库存
        /// 创建人:minjie
        /// 创建时间：2020-09-19
        /// </summary>
        /// <param name="yy">年</param>
        /// <param name="mm">月</param>
        /// <param name="ckbh">仓库编号</param>
        /// <param name="spbh">商品编号</param>
        /// <returns></returns>
        [HttpGet]
        public Rest<zwcl_kczbH_Entity> GetKczbListBySpbh(int yy, int mm, string ckbh, string spbh)
        {
            Rest<zwcl_kczbH_Entity> dto = new Rest<zwcl_kczbH_Entity>();
            string strWhere = "1=1 ";
            if (yy > 0)
                strWhere += " and yy=" + yy;
            if (mm > 0)
                strWhere += " and mm=" + mm;
            if (ckbh != null && ckbh != "")
                strWhere += " and ckbh='" + ckbh + "'";
            if (spbh != null && spbh != "")
                strWhere += " and spbh='" + spbh + "'";
            List<zwcl_kczbH_Entity> ls = bllZwcl_kczbH.GetModelList(strWhere);
            if (ls != null && ls.Count > 0)
            {
                dto.status = 1;
                dto.model = ls[0];
            }
            else
            {
                dto.status = 0;
                dto.msg = "无法查询到该记录";
            }
            return dto;
        }


        [HttpGet]
        public Rest<view_Spda_Entity> GetSpbhByView(string spbh)
        {
            Rest<view_Spda_Entity> dto = new Rest<view_Spda_Entity>();
            view_Spda_Entity model = bllView_Spda.GetModel(spbh);

            if (model != null)
            {
                dto.status = 1;
                dto.model = model;
            }
            else
            {
                dto.status = 0;
                dto.msg = "无法查询到该记录";
            }
            return dto;
        }
        /// <summary>
        /// 更新库存 总数
        /// </summary>
        /// <param name="spbh"></param>
        /// <param name="number"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public Rest SetCkzbBySpbh(string spbh, decimal number)
        {
            Rest dto = new Rest();
            //  我提供一个接口 用来修改修改库存 参数有 商品编号、仓库编号、数量、类型(1 :添加、2:减少)

            int iEnd = 0;//表示未结束
            decimal temp = 0;
            decimal temp2 = 0;
            string strWhere = " yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' ";
            List<zwcl_kczbH_Entity> ls = bllZwcl_kczbH.GetModelList(strWhere);

            List<string> sqlLs = new List<string>();

            if (ls != null)
            {
                var q1 = (from a in ls where a.ckbh == "001" select a).ToList().FirstOrDefault();
                //第一步 减 仓库 001
                if (q1.qmsl > 0)
                {
                    if (q1.qmsl >= number)
                    {
                        iEnd = 2;//已结束
                        var temp3 = q1.qmsl - number;
                        string sql1 = " update zwcl_kczbH set qmsl='" + temp3 + "' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='001'";
                        sqlLs.Add(sql1);
                    }
                    else
                    {
                        iEnd = 1;//部分结束
                        temp = number - q1.qmsl;
                        string sql1 = " update zwcl_kczbH set qmsl='0' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='001'";
                        sqlLs.Add(sql1);
                    }
                }
                //第二步 减 仓库 002
                if (iEnd != 2)
                {
                    var q2 = (from a in ls where a.ckbh == "002" select a).ToList().FirstOrDefault();
                    //仓库1 没有货情况
                    if (iEnd == 0)
                    {
                        if (q2.qmsl > 0)
                        {
                            if (q2.qmsl >= number)
                            {
                                iEnd = 2;//已结束

                                var temp3 = q1.qmsl - number;
                                string sql1 = " update zwcl_kczbH set qmsl='" + temp3 + "' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='002'";
                                sqlLs.Add(sql1);
                            }
                            else
                            {
                                iEnd = 1;//部分结束
                                temp = number - q2.qmsl;
                                string sql1 = " update zwcl_kczbH set qmsl='0' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='002'";
                                sqlLs.Add(sql1);
                            }
                        }
                    }
                    else
                    {
                        if (q2.qmsl >= temp)
                        {
                            iEnd = 2;//已结束
                            var temp3 = q2.qmsl - temp;
                            string sql1 = " update zwcl_kczbH set qmsl='" + temp3 + "' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='002'";
                            sqlLs.Add(sql1);
                        }
                        else
                        {
                            iEnd = 1;//部分结束
                            temp2 = temp - q2.qmsl;
                            string sql1 = " update zwcl_kczbH set qmsl='0' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='002'";
                            sqlLs.Add(sql1);
                        }
                    }

                }
                //第三步 减 仓库 003
                if (iEnd != 2)
                {
                    var q3 = (from a in ls where a.ckbh == "003" select a).ToList().FirstOrDefault();
                    if (iEnd == 0)
                    {
                        if (q3.qmsl > 0)
                        {
                            //直接 削减 库存数量 不考虑是否 大于0
                            var temp3 = q3.qmsl - number;
                            string sql1 = " update zwcl_kczbH set qmsl='" + temp3 + "' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='003'";
                            sqlLs.Add(sql1);
                        }
                    }
                    else
                    {
                        if (temp2 != 0)
                        {
                            var temp3 = q3.qmsl - temp2;
                            string sql1 = " update zwcl_kczbH set qmsl='" + temp3 + "' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='003'";
                            sqlLs.Add(sql1);
                        }
                        else if (temp != 0)
                        {
                            var temp3 = q3.qmsl - temp;
                            string sql1 = " update zwcl_kczbH set qmsl='" + temp3 + "' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='003'";
                            sqlLs.Add(sql1);
                        }
                    }

                }
            }
            if (sqlLs.Count > 0)
            {
                string error = "";
                int ss = bllZwcl_kczbH.ExecuteSqlTran(sqlLs, ref error);

                dto.status = ss;
                dto.msg = error;
            }
            else
            {
                dto.status = 0;
                dto.msg = "操作失败 库存不存在";
            }
            return dto;
        }

        /// <summary>
        /// 退货
        /// </summary>
        /// <param name="spbh"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        [HttpGet]
        public Rest AddCkzbBySpbh(string spbh, decimal number)
        {
            Rest dto = new Rest();

            string sql1 = " update zwcl_kczbH set qmsl=qmsl+'" + number + "' where  yy=YEAR(getdate()) and mm=MONTH(getdate()) and spbh='" + spbh + "' and ckbh='001'";

            int ss = bllZwcl_kczbH.ExecuteSql(sql1);

            dto.status = ss;

            if (ss == 0)
                dto.msg = "操作失败 ，请联系管理员";
            return dto;
        }

        [HttpGet]
        public Rest ExistKhda(string val)
        {
            Rest dto = new Rest();
            string sql = "select   COUNT(1) from jbxx_khdah where khbh='" + val + "' or khmc='" + val + "' or sjhm='" + val + "'";
            string ss = bllJbxx_spdaH.GetSingle(sql);
            if (ss != "" && int.Parse(ss) > 0)

                dto.status = 1;
            else
                dto.msg = "该会员不存在！";

            return dto;
        }

        /// <summary>
        /// 分页获取库存列表(允许带条件)
        /// 创建人:minjie
        /// 创建时间：2020-09-19
        /// </summary>
        /// <param name="pageSize">每页显示数</param>
        /// <param name="pageNumber">当前页</param>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        [HttpGet]
        public RestPage<jbxx_khdaH_Entity> GetKhdaListByWhere(int pageSize, int pageNumber, string strWhere)
        {
            RestPage<jbxx_khdaH_Entity> dto = new RestPage<jbxx_khdaH_Entity>();
            if (!Common2.CheckBadWord(strWhere))
            {
                dto.status = 1;
                int startIndex = (pageNumber - 1) * pageSize + 1;
                int endIndex = pageNumber * pageSize;
                List<jbxx_khdaH_Entity> ls = bllJbxx_khdaH.GetModelList("", "DjLsh desc", startIndex, endIndex);
                int count = bllJbxx_spdaH.GetRecordCount("");
                dto.rows = ls;
                dto.total = count;
            }
            else
            {
                dto.status = 0;
                dto.msg = "条件存在sql注入风险 不允许操作！";
            }
            return dto;
        }

        [HttpPost]
        public Rest AddKhda(string json)
        {
            jbxx_khdaH_Entity model = JsonConvert.DeserializeObject<jbxx_khdaH_Entity>(json);

            string error = "";

            Rest dto = new Rest();
            if (model != null)
            {
                int djlsh = 1;
                model.scbksj = DateTime.Now;
                model.djsj = DateTime.Now;
                model.xgsj = DateTime.Now;

                string bh = "";
                if (!bllJbxx_khdaH.Exists(model.khbh, model.sjhm, ref bh))
                {
                    STDjLsh_Entity dj = bllSTDjLsh.GetModel("JBXX_KHDA");
                    djlsh = dj.DjLsh;
                    model.DjLsh = djlsh;
                    jbxx_khdaH_Entity m1 = bllJbxx_khdaH.GetModel();

                    int khbh = Convert.ToInt32(m1.khbh);

                    model.khbh = (khbh + 1).ToString("00000");

                    if (bllJbxx_khdaH.Add(model, ref error))
                    {
                        bllSTDjLsh.Update("JBXX_KHDA");
                        dto.status = 1;
                        dto.khbh = model.khbh;
                    }

                    else
                        dto.msg = error;
                }
                else
                {
                    dto.status = 0;
                    dto.msg = "客户编号已存在！";
                    dto.khbh = bh;
                }
            }
            else
            {
                dto.status = 0;
                dto.msg = "参数序列化失败！！";
            }

            return dto;
        }

        [HttpPost]
        public Rest InsertYw_pfd(ImportPfd model)
        {
            Rest dto = new Rest();
            int success = 0;
            string msg = "";
            try
            {
                yw_pfdH_Entity m1 = model.model;

                List<yw_pfdB_Entity> _ls = model.cls;

                List<string> sqlList = new List<string>();

                int djlsh = 0;

                if (m1.xsrq == null || m1.xsrq.ToString().Contains("0001"))
                    m1.xsrq = DateTime.Now;
                if (m1.xssj == null || m1.xssj.ToString().Contains("0001"))
                    m1.xssj = DateTime.Now;
                if (m1.hkrq == null || m1.hkrq.ToString().Contains("0001"))
                    m1.hkrq = DateTime.Now;

                if (!bll_yw_pdbH.Exists(m1.xsdh, ref djlsh))
                {
                    int oldDjlsh = m1.DjLsh;//客户端时单据流水号 用来关联销售表体
                    djlsh = bll_yw_pdbH.GetNewLsh();
                    m1.DjLsh = djlsh;

                    string xsdhStr = bll_yw_pdbH.AddStr(m1);
                    sqlList.Add(xsdhStr);//添加表头 插入字符串
                    int r = 1;
                    foreach (yw_pfdB_Entity e1 in _ls)
                    {
                        e1.DjLsh = djlsh;
                        e1.DjBth = r;
                        jbxx_spdaH_Entity sp = bllJbxx_spdaH.GetModel(e1.spbh);
                        if (sp != null)
                        {
                            e1.gysbh = sp.gysbh;
                            e1.gysmc = sp.gysmc;
                            e1.bmbh1 = sp.bmbh;
                            e1.bmmc1 = sp.bmmc;
                            e1.dalsj = sp.lsj;
                        }
                        string xskdbStr = bll_yw_pdbB.AddStr(e1);
                        sqlList.Add(xskdbStr);
                        r++;
                    }
                }
                else
                {
                    int oldDjlsh = m1.DjLsh;//客户端时单据流水号 用来关联销售表体

                    m1.DjLsh = djlsh;

                    string xsdhStr = bll_yw_pdbH.UpdateStr(m1);
                    sqlList.Add(xsdhStr);//添加表头 插入字符串


                    sqlList.Add("delete yw_pdbH where djlsh=" + oldDjlsh);//删除原有标体数据 字符串

                    var q = (from a in _ls where a.DjLsh == oldDjlsh select a).ToList();
                    int j = 1;
                    foreach (yw_pfdB_Entity e1 in q)
                    {
                        e1.DjLsh = djlsh;
                        e1.DjBth = j;
                        string xskdbStr = bll_yw_pdbB.AddStr(e1);
                        sqlList.Add(xskdbStr);//添加表体 插入字符串
                        j++;
                    }


                }

                success = bll_yw_pdbH.ExecuteSqlTran(sqlList, ref msg);


            }
            catch (Exception ex)
            {
                msg = ex.Message;

            }
            dto.status = success;
            dto.msg = msg;
            return dto;
        }



        //[HttpPost]
        //public Rest InsertYw_pfd(ImportPfd model)
        //{
        //    Rest dto = new Rest();
        //    int success = 0;
        //    string msg = "";
        //    try
        //    {
        //        //
        //        List<yw_pfdH_Entity> ls = model.ls;

        //        List<yw_pfdB_Entity> _ls = model.cls;

        //        List<string> sqlList = new List<string>();
        //        //select gysmc,bmbh,bmmc,lsj  from jbxx_spdah where spbh='YGBBJ0006'


        //        foreach (yw_pfdH_Entity m1 in ls)
        //        {
        //            int djlsh = 0;

        //            if (m1.xsrq == null || m1.xsrq.ToString().Contains("0001"))
        //                m1.xsrq = DateTime.Now;
        //            if (m1.xssj == null || m1.xssj.ToString().Contains("0001"))
        //                m1.xssj = DateTime.Now;
        //            if (m1.hkrq == null || m1.hkrq.ToString().Contains("0001"))
        //                m1.hkrq = DateTime.Now;


        //            if (!bll_yw_pdbH.Exists(m1.xsdh, ref djlsh))
        //            {


        //                int oldDjlsh = m1.DjLsh;//客户端时单据流水号 用来关联销售表体
        //                djlsh = bll_yw_pdbH.GetNewLsh();
        //                m1.DjLsh = djlsh;

        //                string xsdhStr = bll_yw_pdbH.AddStr(m1);
        //                sqlList.Add(xsdhStr);//添加表头 插入字符串


        //                var q = (from a in _ls where a.DjLsh == oldDjlsh select a).ToList();

        //                foreach (yw_pfdB_Entity e1 in q)
        //                {
        //                    e1.DjLsh = djlsh;
        //                    jbxx_spdaH_Entity sp = bllJbxx_spdaH.GetModel(e1.spbh);
        //                    if (sp != null)
        //                    {
        //                        e1.gysbh = sp.gysbh;
        //                        e1.gysmc = sp.gysmc;
        //                        e1.bmbh1 = sp.bmbh;
        //                        e1.bmmc1 = sp.bmmc;
        //                        e1.dalsj = sp.lsj;
        //                    }
        //                    string xskdbStr = bll_yw_pdbB.AddStr(e1);
        //                    sqlList.Add(xskdbStr);

        //                }
        //            }
        //            else
        //            {
        //                int oldDjlsh = m1.DjLsh;//客户端时单据流水号 用来关联销售表体

        //                m1.DjLsh = djlsh;

        //                string xsdhStr = bll_yw_pdbH.UpdateStr(m1);
        //                sqlList.Add(xsdhStr);//添加表头 插入字符串


        //                sqlList.Add("delete yw_pdbH where djlsh=" + oldDjlsh);//删除原有标体数据 字符串

        //                var q = (from a in _ls where a.DjLsh == oldDjlsh select a).ToList();
        //                int j = 1;
        //                foreach (yw_pfdB_Entity e1 in q)
        //                {
        //                    e1.DjLsh = djlsh;
        //                    e1.DjBth = j;
        //                    string xskdbStr = bll_yw_pdbB.AddStr(e1);
        //                    sqlList.Add(xskdbStr);//添加表体 插入字符串
        //                    j++;
        //                }


        //            }
        //        }
        //        success = bll_yw_pdbH.ExecuteSqlTran(sqlList, ref msg);


        //    }
        //    catch (Exception ex)
        //    {
        //        msg = ex.Message;

        //    }
        //    dto.status = success;
        //    dto.msg = msg;
        //    return dto;
        //}
    }
}
